<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="上级部门" prop="parentName">
        <el-popover ref="deptListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="deptList"
            :props="deptListTreeProps"
            node-key="deptId"
            ref="deptListTree"
            @current-change="deptListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:deptListPopover
          :readonly="true"
          placeholder="点击选择上级部门"
          class="dept-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="部门昵称" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="部门昵称"></el-input>
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input v-model="dataForm.leader" placeholder="负责人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="部门状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">禁用</el-radio>
          <el-radio :label="0">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        deptId: 0,
        parentId: "",
        ancestors: "",
        deptName: "",
        nickName: "",
        orderNum: "",
        leader: "",
        phone: "",
        email: "",
        status: 0,
        parentName: ""
      },
      deptList: [],
      deptListTreeProps: {
        label: "deptName",
        children: "children"
      },
      dataRule: {
        parentId: [
          { required: true, message: "父部门id不能为空", trigger: "blur" }
        ],
        ancestors: [
          { required: true, message: "祖级列表不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "部门昵称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
        leader: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        email: [{ required: true, message: "邮箱不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "部门状态（0正常 1停用）不能为空",
            trigger: "blur"
          }
        ],
        delFlag: [
          {
            required: true,
            message: "删除标志（0代表存在 2代表删除）不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/dept/select"),
        method: "get",
        params: this.$http.adornParams()
      })
        .then(({ data }) => {
          this.deptList = treeDataTranslate(data.deptList, "deptId");
          console.log(this.deptList);
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (!this.dataForm.id) {
            // 新增
            this.deptListTreeSetCurrentNode();
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/sys/dept/info/${this.dataForm.id}`),
              method: "get",
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.sysDept.parentId;
                this.dataForm.ancestors = data.sysDept.ancestors;
                this.dataForm.deptName = data.sysDept.deptName;
                this.dataForm.nickName = data.sysDept.nickName;
                this.dataForm.orderNum = data.sysDept.orderNum;
                this.dataForm.leader = data.sysDept.leader;
                this.dataForm.phone = data.sysDept.phone;
                this.dataForm.email = data.sysDept.email;
                this.dataForm.status = parseInt(data.sysDept.status);
                this.dataForm.delFlag = data.sysDept.delFlag;
                this.dataForm.createBy = data.sysDept.createBy;
                this.dataForm.createTime = data.sysDept.createTime;
                this.dataForm.updateBy = data.sysDept.updateBy;
                this.dataForm.updateTime = data.sysDept.updateTime;
                this.deptListTreeSetCurrentNode();
              }
            });
          }
        });
    },
    // 菜单树选中
    deptListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.deptId;
      this.dataForm.parentName = data.deptName;
    },

    // 菜单树设置当前选中节点
    deptListTreeSetCurrentNode() {
      this.$refs.deptListTree.setCurrentKey(this.dataForm.parentId);
      this.dataForm.parentName = (this.$refs.deptListTree.getCurrentNode() ||
        {})["deptName"];
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dept/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              deptId: this.dataForm.id || undefined,
              parentId: this.dataForm.parentId,
              ancestors: this.dataForm.ancestors,
              deptName: this.dataForm.deptName,
              nickName: this.dataForm.nickName,
              orderNum: this.dataForm.orderNum,
              leader: this.dataForm.leader,
              phone: this.dataForm.phone,
              email: this.dataForm.email,
              status: this.dataForm.status,
              delFlag: this.dataForm.delFlag,
              createBy: this.dataForm.createBy,
              createTime: this.dataForm.createTime,
              updateBy: this.dataForm.updateBy,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="scss">
.mod-dept {
  .dept-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
